package mq.rabbitmq.returnlistener;

import com.rabbitmq.client.Channel;
import mq.rabbitmq.Constant;
import mq.rabbitmq.RabbitMqChannelFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * return listener 用于处理一些不可路由的消息。
 * 在发送消息的时候，当前的exchange不存在或者制定的路由key路由不到，这个时候如果我们需要监听这种不可达的消息
 *
 * @author verlet
 * @date 2019/12/29
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Channel channel = RabbitMqChannelFactory.createChannel();
        String msg = " hello rabbitMQ return Message";
        channel.addReturnListener((replyCode, replyText, exchange, routingKey, properties, body) -> {
            System.out.println("==========ReturnListener================");
            System.out.println("replyCode=" + replyCode);
            System.out.println("replyText=" + replyText);
            System.out.println("exchange=" + exchange);
            System.out.println("routingKey=" + routingKey);
            System.out.println("properties=" + properties);
            System.out.println("body=" + new String(body));
        });
        channel.basicPublish(Constant.RETURN_EX_CHANGE, Constant.RETURN_ROUTING_KEY, true, null, msg.getBytes());
        System.out.println("添加一条消息成功");
        Thread.sleep(1000);
        // 关闭连接
        RabbitMqChannelFactory.close(channel);
    }
}
